package com.example.platformhimybodymanage.mqreciever;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.example.platformhimybodymanage.config.rabbitmq.RabbitMQConfig;
import com.example.platformhimybodymanage.mqrsend.Sender;
import com.rabbitmq.client.Channel;

/**
 * 消费
 * @author Dhjie
 *
 */
@Component
public class Reciever {

private static final Logger logger=LoggerFactory.getLogger(Sender.class);
	
	
	@RabbitHandler
	@RabbitListener(queues= { RabbitMQConfig.DIRCT_QUEUE_PAY })
	public void dircePayReciever(Channel channel,Message message) {
		String json=new String(message.getBody());
		logger.info("DIRCT_QUEUE_PAY队列收到消息："+json);
		try {
			//限流 在非自动确认情况下，一定数目的消息没有确认，不进行消费新的消息
			// prefetchSize：消费消息的大小，0表示不做限制；
			// prefetchCount：同时消费消息的数量；
			// global：限流策略应用的级别，true：Channel级别；false：consumer级别；
			/*channel.basicQos(0, 1, false);*/
			// 消费者，要想做限流必须将自动ack设置为false
			/*channel.basicConsume(MqConfig.QUEUE_NAME,reciever);*/
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	
	@RabbitHandler
	@RabbitListener(queues= {RabbitMQConfig.FANOUT_QUEUE_BEIJING} )
	public void fanoutBJReciever(com.rabbitmq.client.Channel channel,Message message) {
		String json = new String(message.getBody());
		System.err.println("FANOUT_QUEUE_BEIJING队列收到信息：" + json);
		try {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	@RabbitHandler
	@RabbitListener(queues= {RabbitMQConfig.FANOUT_QUEUE_TIANJIN} )
	public void fanoutTJReciever(com.rabbitmq.client.Channel channel,Message message) {
		String json = new String(message.getBody());
		System.err.println("FANOUT_QUEUE_TIANJIN队列收到信息：" + json);
		try {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
